--[[--[[*
-- * 
 * 转移伤害
-- *
-- */]]

---@class BuffEffect454100 : BaseBuffEffect
BuffEffect454100 = ClientFight.CreateClass("BuffEffect454100", ClientFight.BaseBuffEffect)
local t = BuffEffect454100

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = {...};
    local source = buff.source;
    local target = buff.target;
    local fightResult = parameters[3];
    local buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local transferDamageRatio = buffEffectValue[1];

    if (fightResult.getDamage() <= 0) then
        return IBuffEffect.FAIL;
    end
    -- triggerSkill(fight,(Fighter)parameters[0],buff.getTarget(),buffId1);
    -- return super.action(fight, buff, parameters);
    --[[
    for (int i = 3;i < buffEffectValue.length; i++) then
    int buffId2 = buffEffectValue[i]; -- 因果印记
    FighterBuff existingBuff = BuffManager.getBuffByModel(source, buffId2);
    if (existingBuff != nil) then
    transferDamageRatio += existingBuff.buffBean.f_BuffEffectValue[0] * existingBuff.getOverlay();
    break;
    end

    end
    long transferDamage = calcRatio(fightResult.getDamage(), transferDamageRatio);
    ]]
    if (table.getn(buffEffectValue) > 3) then
        if (math.random(1, 10001) <= buffEffectValue[4]) then
            --额外触发概率
            EventManager.addDelayedEvent(fight, fight.frame + buffEffectValue[5] / Global.FIGHT_FRAME, function()
                t:trigger(fight, buff);
            end);
        end
    end
    if (not t:trigger(fight, buff)) then
        return IBuffEffect.FAIL;
    end
    --        buffManager.addBuff(fight, buff.getSkill(), source, source, false, buffId2, 1); --不能再给自己叠印记了
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

function t:trigger(fight, buff)
    local source = buff.source;
    local target = buff.target;
    if (source == nil or target == nil) then
        return false;
    end
    local buffEffectValue = buff.buffBean.f_BuffEffectValue;
    local transferRadius = Utils.toRatio(buffEffectValue[2]);
    local buffId1 = buffEffectValue[3]; -- 生命移除
    local selectAttackTargets = ClientFight.SkillManager.selectAttackTargets(fight, target, buff.skill.skill, SkillTargetRangeEnum.IN_ATTACK_RANGE, SkillTargetTypeEnum.FRIEND, transferRadius, nil, target.position, nil, nil);

    if (table.getn(selectAttackTargets) == 0) then
        return false;
    end

    for k, fighter in pairs(selectAttackTargets) do
        ClientFight.BaseBuffEffect.triggerSkill(self,fight, source, fighter, buffId1, buff.target);
    end
    return true;
end

t.New()